package com.cw.jeeyt.dao.impl.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cw.jeeyt.dao.intf.security.SecurityResourceDao;
import com.cw.jeeyt.dao.mapper.security.SecurityResourceMapper;
import com.cw.jeeyt.dao.model.security.SecurityResourceDo;
import com.cw.jeeyt.dao.model.security.SecurityResourceKeyDo;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 资源信息 dao数据库操作接口
 *
 * @author chenw
 * @version 1.0.0
 */
@Repository
public class SecurityResourceDaoImpl implements SecurityResourceDao {
    @Autowired
    private SecurityResourceMapper securityResourceMapper;

    /**
     * 删除资源信息
     *
     * @param resource
     */
    @Override
    public void delete(SecurityResourceDo resource) {
        securityResourceMapper.delete(resource);
    }

    /**
     * 修改资源信息
     *
     * @param resource
     */
    @Override
    public void update(SecurityResourceDo resource) {
        securityResourceMapper.updateByPrimaryKeySelective(resource);
    }

    /**
     * 根据主键查询资源信息
     *
     * @param id 主键id
     * @return 资源信息
     */
    @Override
    public SecurityResourceDo getById(Long id) {
        SecurityResourceKeyDo keyDo = new SecurityResourceKeyDo();
        keyDo.setId(id);
        return securityResourceMapper.selectByPrimaryKey(keyDo);
    }

    /**
     * 新增资源信息
     *
     * @param resource
     * @return 整数
     */
    @Override
    public Integer insert(SecurityResourceDo resource) {
        return securityResourceMapper.insertSelective(resource);
    }

    /**
     * 查询所有资源信息
     *
     * @return 资源信息集合
     */
    @Override
    public ArrayList<SecurityResourceDo> queryResourceList() {
        ArrayList<SecurityResourceDo> resourceDos = securityResourceMapper.queryResourceList();

        return resourceDos;
    }

    /**
     * 分页查询资源信息
     *
     * @param resource 查询条件
     * @param startNum 开始条数
     * @param pageSize 每页长度
     * @return 资源信息集合
     */
    @Override
    public PageInfo<SecurityResourceDo> queryByPage(SecurityResourceDo resource, Integer startNum, Integer pageSize)
    {
        PageHelper.startPage(PageUtil.calPageNum(startNum, pageSize), pageSize);
        return new PageInfo<>(securityResourceMapper.queryByPage(resource));
    }

    /**
     * 根据用户名称查询用户资源
     *
     * @param userName
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityResourceDo> getResourceByUserName(String userName) {
        return securityResourceMapper.getResourceByUserName(userName);
    }

    /**
     * 根据角色id查询资源信息
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public ArrayList<SecurityResourceDo> getResourceByRoleId(Long roleId) {
        return securityResourceMapper.getResourceByRoleId(roleId);
    }

    /**
     * 加载根节点资源信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public SecurityResourceDo queryRootSecurityResource() {
        return securityResourceMapper.queryRootSecurityResource();
    }

    /**
     * 加载父节点下的所有资源信息
     *
     * @param resId
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityResourceDo> querySecurityResourceListByParentId(Long resId) {
        return securityResourceMapper.querySecurityResourceListByParentId(resId);
    }

    /**
     * 根据用户角色加载资源信息
     *
     * @param roles
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityResourceDo> getResourcesByUserRoles(String[] roles) {
        return securityResourceMapper.getResourcesByUserRoles(roles);
    }

    /**
     * 根据用户ID查询用户权限信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List<SecurityResourceDo> getResourcesByUserId(Long userId) {
        return securityResourceMapper.getResourcesByUserId(userId);
    }

    /**
     * 更新父节点id
     *
     * @param id
     * @param parentId
     * @throws Exception
     */
    @Override
    public void updateResourceParentId(Long id, Long parentId) {
        securityResourceMapper.updateResourceParentId(id, parentId);
    }

    /**
     * 更新资源名称
     *
     * @param id
     * @param resName
     * @throws Exception
     */
    @Override
    public void updateResourceName(Long id, String resName) {
        securityResourceMapper.updateResourceName(id, resName);
    }
    @Override
    public PageInfo<SecurityResourceDo> querySelectSecurityResource(SecurityResourceDo resource, Integer startNum,
        Integer pageSize) {
        PageHelper.startPage(PageUtil.calPageNum(startNum, pageSize), pageSize);
        return new PageInfo<>(securityResourceMapper.querySelectSecurityResource(resource));
    }

    /**
     * 删除父节点下所有的资源
     *
     * @param parentId
     */
    @Override
    public void deleteByParentId(Long parentId) {
        securityResourceMapper.deleteByParentId(parentId);
    }

    /**
     * 查询父id下的所有按钮资源
     *
     * @param parentId
     * @return
     */
    @Override
    public List<SecurityResourceDo> queryBtnListByParentId(Long parentId) {
        return securityResourceMapper.queryBtnListByParentId(parentId);
    }

    /**
     * 根据资源名称及父id查询资源信息
     *
     * @param resName
     * @param parentId
     * @return
     */
    @Override
    public SecurityResourceDo getResByResNameAndParentId(String resName, Long parentId) {
        return securityResourceMapper.getResByResNameAndParentId(resName, parentId);
    }

    /**
     * 根据父id及资源名称更新按钮资源url
     *
     * @param btnRes
     */
    @Override
    public void updateResUrlByResTitleAndParentId(SecurityResourceDo btnRes) {
        securityResourceMapper.updateResUrlByResTitleAndParentId(btnRes);
    }

    /**
     * 批量新增资源信息
     *
     * @param srList
     */
    @Override
    public void insertBat(List<SecurityResourceDo> srList) {
        securityResourceMapper.insertBat(srList);
    }

    /**
     * 判断资源是否需要权限访问
     *
     * @param url
     * @return
     */
    @Override
    public Integer checkResourceNeedAuth(String url) {
        return securityResourceMapper.checkResourceNeedAuth(url);
    }

    @Override
    public void updateBtnList(Long id, String btnList) {
        securityResourceMapper.updateBtnList(id,btnList);
    }
}
